﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace DemoExcelWorkbook
{
    public partial class Sheet1
    {
        private void Sheet1_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet1_Shutdown(object sender, System.EventArgs e)
        {
        }

        private int lastFocusedColumn = -1;
        private int lastFocusedRow = -1;
        private int defaultColor = -1;
        public void Focus(int column, int row)
        {
            if (defaultColor == -1)
            {
                defaultColor = ((Excel.Range)Cells[row, column]).Interior.Color;
            }
            if (lastFocusedColumn >= 0)
            {
                ((Excel.Range)Cells[lastFocusedRow, lastFocusedColumn]).Interior.Color = defaultColor;
            }
            ((Excel.Range)Cells[row, column]).Interior.Color = 255;
            lastFocusedColumn = column;
            lastFocusedRow = row;
        }

        public void SetNumber(int column, int row, int number)
        {
            int current = 0;
            if (((Excel.Range)Cells[row, column]).Value2 != null)
            {
                current = Int32.Parse(((Excel.Range)Cells[row, column]).Value2);
            }
            ((Excel.Range)Cells[row, column]).Value2 = current + number;
        }

        public void MultNumber(int column, int row, int mult)
        {
            int current = 0;
            if (((Excel.Range)Cells[row, column]).Value2 != null)
            {
                current = Int32.Parse(((Excel.Range)Cells[row, column]).Value2);
            }
            ((Excel.Range)Cells[row, column]).Value2 = current * mult;
        }

        #region VSTO デザイナーで生成されたコード

        /// <summary>
        /// デザイナーのサポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet1_Startup);
            this.Shutdown += new System.EventHandler(Sheet1_Shutdown);
        }

        #endregion

    }
}
